package com.wl2o2o.javau8g;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实现集合排序的几种方式
 * @Author <a href="https://github.com/wl2o2o">程序员CSGUIDER</a>
 * @From <a href="https://wl2o2o.github.io">CSGUIDER博客</a>
 * @CreateTime 2024/4/15
 */

public class CollectionSort {
    public static class Student implements Comparable<Student>{
        private String name;

        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student {" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        // 重写比较规则：按姓名升序，姓名相同按年龄升序
        @Override
        public int compareTo(Student o) {
            int flag = this.name.compareTo(o.name);
            if (flag == 0) {
                return this.age - o.age;
            } else {
                return flag;
            }
        }
    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 20));
        students.add(new Student("Bob", 22));
        students.add(new Student("Judy", 18));
        students.add(new Student("cS", 18));
        students.add(new Student("Alice", 23));
        students.add(new Student("Charlie", 20));

        System.out.println("Before sorting:");
        for (Student student : students) {
            System.out.println(student);
        }

        // 方式一：实现 Compareable 接口，实现 compareTo 方法
        // Collections.sort(students);
        // 方式二：使用 Comparator 接口，实现 compare 方法
        // Collections.sort(students, (o1, o2) -> {
        //     int flag = o1.name.compareTo(o2.name);
        //     if (flag == 0) {
        //         // 升序
        //         return o2.age - o1.age;
        //     } else {
        //         return flag;
        //     }
        // });

        // 方式二优化：（名字降序）
        // Collections.sort(students, Comparator.comparing(Student::getName).reversed().thenComparing(Student::getAge));

        // 方式三：使用 Stream API + Student 类实现 Comparator 接口，重写 compareTo 方法
        // List<Student> collect = students.stream().sorted().collect(Collectors.toList());
        // collect.forEach(System.out::println);

        // 方式四：使用 Stream API + 自实现 Comparator 接口
        students.stream().sorted((o1, o2)->{
            int flag = o1.name.compareTo(o2.name);
            if (flag == 0) {
                return o1.age - o2.age;
            } else {
                return flag;
            }
        }).collect(Collectors.toList()).forEach(System.out::println);

        // 方式四优化：
        // List<Student> collect = students.stream().sorted(Comparator.comparing(Student::getName).thenComparing(Student::getAge)).collect(Collectors.toList());
        // System.out.println("After sorting:");
        // collect.forEach(System.out::println);

        System.out.println("After sorting:");
        for (Student student : students) {
            System.out.println(student);
        }

        System.out.println("All tests passed.");




    }

}
